using
directives add additional scopes to the set of scopes searched during name lookup. All identifiers in these scopes become
visible, increasing the possibility that the identifier found by the compiler does not meet developer expectations.
Using-declarations or fully-qualified names restricts the set of names considered to only the name explicitly specified, and so these are
safer options.
Exceptions
It is not easy to fully qualify the content of the std::literals
and std::placeholders
namespaces. Therefore, this rule
does not raise violations for using
directives that target these namespaces or their sub-namespaces.
Noncompliant code example
namespace NS1 {
int f();
}
using namespace NS1; // Noncompliant
void g() {
f();
}
Compliant solution
namespace NS1 {
int f();
}
void g() {
NS1::f();
}
// Or
using NS1::f; // Compliant, this is a using declaration
void g() {
f();
}